﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace zframe.json
{
    
    public class UnityJsonDict<TKey,TVal>:ISerializationCallbackReceiver
    {
        [SerializeField]
        private List<TKey> Keys = new List<TKey>();
        [SerializeField]
        private List<TVal> Values = new List<TVal>();

        public Dictionary<TKey, TVal> Target; 


        public UnityJsonDict(Dictionary<TKey, TVal> dict)
        {
            this.Target = dict;
             
        }

        void ISerializationCallbackReceiver.OnBeforeSerialize()
        {
            Keys = Target.Keys.ToList();
            Values = Target.Values.ToList();
        }

        void ISerializationCallbackReceiver.OnAfterDeserialize()
        {
            Target = new Dictionary<TKey, TVal>();
            for (int i = 0; i < Keys.Count; i++)
            {
                Target.Add(Keys[i], Values[i]); 
            }
        }
    }
}
